<template>
  <div class="user-home">
    <!-- 移除轮播图 -->

    <el-row :gutter="20" class="section">
      <el-col :span="24">
        <h2 class="section-title">热门商品</h2>
      </el-col>
      <el-col :span="6" v-for="product in hotProducts" :key="product.id">
        <el-card class="product-card" @click="goToProductDetail(product.id)">
          <div class="product-info">
            <div class="product-name">{{ product.name }}</div>
            <div class="product-category">{{ getCategoryName(product.categoryId) }}</div>
            <div class="product-price">¥{{ product.price }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="section">
      <el-col :span="24">
        <h2 class="section-title">最新商品</h2>
      </el-col>
      <el-col :span="6" v-for="product in newProducts" :key="product.id">
        <el-card class="product-card" @click="goToProductDetail(product.id)">
          <div class="product-info">
            <div class="product-name">{{ product.name }}</div>
            <div class="product-category">{{ getCategoryName(product.categoryId) }}</div>
            <div class="product-price">¥{{ product.price }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getProducts } from '@/api/user'
import { getAllCategories } from '@/api/category'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 移除轮播图数据

// 热门商品
const hotProducts = ref([])

// 最新商品
const newProducts = ref([])

// 分类列表
const categoryList = ref([])

// 获取分类列表
const fetchCategoryList = async () => {
  try {
    const response = await getAllCategories()
    if (response.code === 200) {
      categoryList.value = response.data
    }
  } catch (error) {
    console.error('获取分类列表失败:', error)
  }
}

// 获取分类名称
const getCategoryName = (categoryId) => {
  if (!categoryId) return '未分类'
  const category = categoryList.value.find(item => item.id === categoryId)
  return category ? category.name : '未知分类'
}

// 获取商品数据
onMounted(async () => {
  try {
    // 获取分类列表
    await fetchCategoryList()

    // 获取热门商品
    const hotResponse = await getProducts({ pageNum: 1, pageSize: 4 })
    if (hotResponse.code === 200) {
      hotProducts.value = hotResponse.data.records
    }

    // 获取最新商品
    const newResponse = await getProducts({ pageNum: 1, pageSize: 4 })
    if (newResponse.code === 200) {
      newProducts.value = newResponse.data.records
    }
  } catch (error) {
    console.error('获取商品数据失败:', error)
    ElMessage.error('获取商品数据失败')
  }
})

// 跳转到商品详情
const goToProductDetail = (id: number) => {
  router.push(`/user/products?id=${id}`)
}
</script>

<style scoped>
.user-home {
  padding: 20px 0;
}

.section {
  margin-top: 30px;
}

.section-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
  border-left: 4px solid #409EFF;
  padding-left: 10px;
}

.product-card {
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.product-info {
  padding: 10px;
}

.product-name {
  font-size: 16px;
  margin-bottom: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-category {
  font-size: 12px;
  color: #909399;
  background-color: #f5f7fa;
  padding: 2px 8px;
  border-radius: 10px;
  display: inline-block;
  margin-bottom: 5px;
}

.product-price {
  font-size: 18px;
  color: #f56c6c;
  font-weight: bold;
}
</style>
